import { IComponentInfo, ISvgDataLists, ISvgCanvas, ILeftImgLists, IMouseInfo, ISelectSvg } from "../Model";
/**
 * 组件上移
 * @param svgLists 
 * @param select_svg 
 */
export function moveUp(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    svgLists[select_svg.index].svgPositionY -= 1;
}
/**
 * 组件下移
 * @param svgLists 
 * @param select_svg 
 */
export function moveDown(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    svgLists[select_svg.index].svgPositionY += 1;
}
/**
 * 组件左移
 * @param svgLists 
 * @param select_svg 
 */
export function moveLeft(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    svgLists[select_svg.index].svgPositionX -= 1;
}
/**
 * 组件右移
 * @param svgLists 
 * @param select_svg 
 */
export function moveRight(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    svgLists[select_svg.index].svgPositionX += 1;
}
/**
 * 组件复制
 * @param svgLists 
 * @param select_svg 
 */
export function hotkeyCopy(svgLists: ISvgDataLists[], select_svg: ISelectSvg, select_svg_list: ISvgDataLists[]) {
    console.log('复制', select_svg_list)
    let select_svg_cope_list: ISvgDataLists[] = []
    //优先鼠标框选
    if (select_svg_list.length) {
        select_svg_list.forEach(e => {
            let svg = {
                ...(JSON.parse(JSON.stringify(e))),
                id: `${new Date().getTime()}` + `${Math.floor(Math.random() * 101)}`,
                svgPositionX: e.svgPositionX + 100,
                svgPositionY: e.svgPositionY + 50,
                title: e.title + `-copy`
            }
            svgLists.push(svg)
            select_svg_cope_list.push(svg)
        });
        select_svg_list.length = 0//vue3 要这样清空数组
        select_svg_list.push(...select_svg_cope_list)//vue3 要这样赋值，或者rective里面要包一层
    }
    else {
        svgLists.push({
            ...(JSON.parse(JSON.stringify(svgLists[select_svg.index]))),
            id: `${new Date().getTime()}` + Math.floor(Math.random() * 101),
            svgPositionX: svgLists[select_svg.index].svgPositionX + 10,
            svgPositionY: svgLists[select_svg.index].svgPositionY + 10,
            title: svgLists[select_svg.index].title + `-copy`
        })
    }

}
/**
 * 组件删除
 * @param svgLists 
 * @param select_svg 
 */
export function hotkeyDel(svgLists: ISvgDataLists[], select_svg: ISelectSvg, select_svg_list: ISvgDataLists[]) {
    if (select_svg_list.length > 0 && select_svg.id === 'mouseRect') {
        for (let i = 0; i < svgLists.length; i++) {
            let spliceFlag = false
            for (const e of select_svg_list) {
                if (svgLists[i].id === e.id) {
                    {
                        svgLists.splice(i, 1);//splice的时候，索引要向前移动，不然会少循环
                        spliceFlag = true
                    }

                }
            }
            if (svgLists[i]?.id === 'mouseRect') {
                svgLists[i].svgPositionX = 0
                svgLists[i].svgPositionY = 0
                svgLists[i].extend_attr.wh.width = 0
                svgLists[i].extend_attr.wh.height = 0
                svgLists.splice(i, 1)
                spliceFlag = true
            }
            if (spliceFlag)
                i--
        }
        select_svg_list.length = 0//框选消失的时候清除select列表

    }
    else {
        if (select_svg && svgLists)
            svgLists.splice(select_svg?.index, 1);
    }

}
/**
 * 组件置于顶层
 * @param svgLists 
 * @param select_svg 
 */
export function hotkeyPutOnTop(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    const temp = svgLists[select_svg.index];
    svgLists.splice(select_svg.index, 1);
    svgLists.push(temp);
    select_svg.index = svgLists.length - 1;
}
/**
 * 组件置于底层
 * @param svgLists 
 * @param select_svg 
 */
export function hotkeyPutOnButtom(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    const temp = svgLists[select_svg.index];
    svgLists.splice(select_svg.index, 1);
    svgLists.unshift(temp);
    select_svg.index = 0;
}
/**
 * 组件上移一层
 * @param svgLists 
 * @param select_svg 
 */
export function hotkeyPutOnUp(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    if (svgLists.length === 1 || select_svg.index === svgLists.length - 1) {
        return;
    }
    const temp = svgLists[select_svg.index];
    svgLists[select_svg.index] = svgLists[select_svg.index + 1];
    svgLists[select_svg.index + 1] = temp;
    select_svg.index += 1;
}
/**
 * 组件下移一层
 * @param svgLists 
 * @param select_svg 
 */
export function hotkeyPutOnDown(svgLists: ISvgDataLists[], select_svg: ISelectSvg) {
    if (svgLists.length === 1 || select_svg.index === 0) {
        return;
    }
    const temp = svgLists[select_svg.index];
    svgLists[select_svg.index] = svgLists[select_svg.index - 1];
    svgLists[select_svg.index - 1] = temp;
    select_svg.index -= 1;
}